package co2.bean;

import java.io.IOException;
import java.io.ObjectInputStream;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public abstract class AbstractBean {
	
	
	@PostConstruct
	public void beforePostConstruct(){
		getApplicationContext().
        getAutowireCapableBeanFactory().
        autowireBean(this);
	}
	
	private void readObject(ObjectInputStream ois)
            throws ClassNotFoundException, IOException {
		ois.defaultReadObject();
		getApplicationContext().
        getAutowireCapableBeanFactory().
        autowireBean(this);
	}
	

	public static ApplicationContext getApplicationContext(){
		ServletContext servletContext = getServletContext();
		return WebApplicationContextUtils.getRequiredWebApplicationContext(
				servletContext);
	}
	

	public static ServletContext getServletContext(){
		return (ServletContext) FacesContext
	    .getCurrentInstance().getExternalContext().getContext();
	}

}
